package cn.fanyc.action.blockchain.fbc.browser.controller.contract;

import cn.fanyc.action.blockchain.fbc.browser.config.GlobalConstants;
import org.hyperledger.fabric.gateway.Gateway;
import org.hyperledger.fabric.sdk.Channel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author Fanyc
 * @date 2020-10-18 19:43
 */
@Controller
@RequestMapping("/contract")
public class ContractUIController {

    /**
     * 模板页面文件夹
     */
    public static final String prefix = "contract";

    /**
     * Fabric网关
     */
    @Autowired
    private Gateway gateway;

    /**
     * 首页
     *
     * @param modelMap 视图
     * @return 页面
     */
    @RequestMapping({"", "/", "/index"})
    public String index(ModelMap modelMap) {
        // 获取通道
        Channel channel = gateway.getNetwork(GlobalConstants.CHANNEL_NAME).getChannel();
        // 获取合约名称列表
        modelMap.addAttribute("ccObjs", channel.getDiscoveredChaincodeNames());
        return prefix + "/index";
    }
}
